<template lang="pug">
el-dialog(
  :visible.sync="show"
  title="去发货"
  width="570px"
  @open="beforeOpen"
  :before-close="handleClose"
  :close-on-click-modal="false"
)
  .modal-body
    .form-radio
      el-radio(v-model="form.type", :label="1") 自行配送
      el-radio(v-model="form.type", :label="2") 物流配送
    .form-item(v-if="form.type == 1")
      .form-item-label.important 预计配送时间
      .form-item-bd
        el-date-picker(placeholder="选择日期" v-model="form.deliveryDate" type="date" value-format="yyyy-MM-dd HH:mm:ss")
    .form-item(v-if="form.type == 2")
      .form-item-label.important 物流公司
      .form-item-bd
        el-input(placeholder="请输入物流公司名称" v-model="form.courierCompany")
    .form-item(v-if="form.type == 2")
      .form-item-label.important 物流单号
      .form-item-bd
        el-input(placeholder="请输入物流单号" v-model="form.courierNumber")
    .form-item.form-item-top(v-if="form.type == 2")
      .form-item-label 物流单照片
      .form-item-bd.form-item-bd-flex
        el-upload(
          action="#"
          class="upload-demo"
          :multiple="false"
          accept="image/png, image/jpeg, image/jpg, image/gif"
          ref="upload"
          :show-file-list="false"
          :auto-upload="false"
          :on-change="handleChange"
        )
          .form-item-upload(slot="trigger")
            .el-icon-plus
            .form-item-upload-info 上传图片
        .form-item-img(v-if="form.fileManage.standardImg")
          img(:src="form.fileManage.standardImg | filterSrc")
  .modal-footer(slot="footer")
    el-button(@click="handleClose") 取消
    el-button(type="primary" @click="confirm") 确定
</template>

<script>
import { FileUpload, saveFileManage, deliveryProduct } from 'api'
export default {
  name: 'deliveryModal',
  props: {
    show: Boolean,
    orderId: [Number, String]
  },
  data () {
    return {
      form: {
        orderId: '',
        type: 1,
        deliveryDate: '',
        courierCompany: '',
        courierNumber: '',
        fileManage: {}
      }
    }
  },
  methods: {
    beforeOpen () {
      this.form =  {
        orderId: this.orderId,
        type: 1,
        deliveryDate: '',
        courierCompany: '',
        courierNumber: '',
        fileManage: {}
      }
    },
    handleClose () {
      this.$emit('close')
    },
    async handleChange (file, fileList) {
      try {
        let fileObj = await this.fileUpload(file.raw)
        this.form.fileManage = fileObj
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    async fileUpload (raw) {
      return new Promise(async (resolve, reject) => {
        try {
          let formData = new FormData()
          formData.append('Filedata', raw)
          formData.append("applicationId", 4)
          const { ResultSet } = await this.$axios.post(FileUpload, { formData: formData, isUpload: true }, { headers:{ "Content-Type": "multipart/form-data" } })
          resolve({ ...ResultSet.Result[0] })
        } catch (err) {
          console.log(err)
          reject(err)
        }
      })
    },
    async confirm () {
      try {
        if (this.form.type === 1) {
          delete this.form.courierCompany
          delete this.form.courierNumber
          delete this.form.fileManage
          if (!this.form.deliveryDate) {
            this.$message.error('请先选择预计配送时间！')
            return
          }
        } else {
          delete this.form.deliveryDate
          if (!this.form.courierCompany) {
            this.$message.error('请输入物流公司名称！')
            return
          }
          if (!this.form.courierNumber) {
            this.$message.error('请输入物流单号！')
            return
          }
        }
        await this.$axios.post(deliveryProduct, { content: { ...this.form } })
        this.$emit('success')
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
  }
} 
</script>

<style lang="scss" scoped>
.form-radio {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
}
.form-item {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  &.form-item-top {
    align-items: flex-start;
  }
}
.form-item-label {
  color: #363636;
  font-size: 14px;
  min-width: 100px;
  &.important {
    &:before {
      content: '*';
      color: #FF2E42;
      margin-right: 4px;
    }
  }
}
.form-item-bd-flex {
  display: flex;
}
.form-item-bd {
  flex: 1;
  .el-date-editor.el-input {
    width: 100%;
  }
}
.form-item-upload {
  width: 64px;
  height: 64px;
  background: #FFFFFF;
  border: 1px solid #CBCBCB;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  cursor: pointer;
  .el-icon-plus {
    font-size: 26px;
    color: #FF2E42;
    font-weight: bold;
  }
  .form-item-upload-info {
    margin-top: 5px;
    font-size: 12px;
    color: #C2C2C2;
  }
}
</style>
